package com.gitee.sop.layui.adminserver.module.background.admin.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gitee.sop.layui.adminserver.entrance.bo.ServiceContext;
import com.gitee.sop.layui.adminserver.module.background.admin.entity.SysApplication;
import com.gitee.sop.layui.adminserver.module.background.admin.entity.SysMenuRole;
import com.gitee.sop.layui.adminserver.module.background.admin.entity.SysUser;
import com.gitee.sop.layui.adminserver.module.background.admin.entity.SysUserMenuRoleRelation;
import com.gitee.sop.layui.adminserver.module.background.admin.manager.SysUserMenuRoleRelationManager;
import com.gitee.sop.layui.adminserver.module.background.admin.mapper.SysUserMenuRoleRelationMapper;
import com.gitee.sop.layui.adminserver.module.background.admin.service.SysDataModifyLockService;
import com.gitee.sop.layui.adminserver.module.background.admin.service.SysMenuRoleService;
import com.gitee.sop.layui.adminserver.module.background.admin.service.SysUserMenuRoleRelationService;
import com.gitee.sop.layui.adminserver.module.background.admin.util.SysDataModifyLockUtil;
import com.gitee.sop.layui.adminserver.module.base.util.ObjectUtil;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * <p>
 * 用户角色mapping 服务实现类
 * </p>
 *
 * @author admin
 * @since 2019-11-13
 */
@Service
public class SysUserMenuRoleRelationServiceImpl extends ServiceImpl<SysUserMenuRoleRelationMapper, SysUserMenuRoleRelation> implements SysUserMenuRoleRelationService {
    @Lazy
    @Resource
    private SysDataModifyLockService sysDataModifyLockService;
    @Lazy
    @Resource
    private SysMenuRoleService sysMenuRoleService;
    @Lazy
    @Resource
    private SysUserMenuRoleRelationManager sysUserMenuRoleRelationManager;

    @Override
    public List<String> listRoleUuidByUserUuid(String appId, String userUuid) {
        return this.baseMapper.listRoleUuidByUserUuid(appId, userUuid);
    }

    @Override
    public void setAuthority(String userUuid, List<Integer> roleIdList, String appId) {
        sysDataModifyLockService.checkLocked(SysDataModifyLockUtil.generateKey(SysUser.class, userUuid));

        SysApplication curApplication = ServiceContext.me().getCurrentApplication();
        String realAppId = ObjectUtil.isEmpty(appId) ? curApplication.getAppId() : appId;

        //当前用户
        SysUser sysUser = ServiceContext.me().checkedUserAndGet(SysUser.class);

        //删除时，只能删除当前用户有权限操作的对应关系，即角色由其创建且在同一个应用内
        baseMapper.deleteByUserUuid(realAppId, userUuid, sysUser.getUuid());

        if (!roleIdList.isEmpty()) {

            //TODO 对roleUuid权限进行判断，检索当前用户是否可赋值这些权限，防止恶意攻击，刷取权限功能

            List<SysMenuRole> sysMenuRoles = this.sysMenuRoleService.getListByIds(appId, roleIdList);

            List<SysUserMenuRoleRelation> list = sysMenuRoles.stream().collect(ArrayList::new, (res, ele) -> res.add(new SysUserMenuRoleRelation(realAppId, userUuid, ele.getUuid())), (l, r) -> {
            });
            saveBatch(list);
        }

        //清空缓存
        this.sysUserMenuRoleRelationManager.clearCache();
    }

    @Override
    public Set<String> getPermissionUrl(String appId, String userUuid) {
        return this.sysUserMenuRoleRelationManager.getMenuUrlListByUserUuid(appId, userUuid);
    }

    @Override
    public int countInMenuRoleUuids(String appId, List<String> menuRoleUuidList) {
        return baseMapper.countInMenuRoleUuids(appId, menuRoleUuidList);
    }

    @Override
    public void removeByUserUuid(String appId, String uuid) {
        //当前用户
        SysUser sysUser = ServiceContext.me().checkedUserAndGet(SysUser.class);

        //删除时，只能删除当前用户有权限操作的对应关系，即角色由其创建且在同一个应用内
        baseMapper.deleteByUserUuid(appId, uuid, sysUser.getUuid());

        //清空缓存
        this.sysUserMenuRoleRelationManager.clearCache();
    }

}
